Automation Test Practice

Follow me on GitHub

Interface Static Method and Default Method

I started to learn JAVA 20 years ago when I was a student in the university. After that in so many years I had kept remembering JAVA interface can only be used to define method, not implementation. However things changed after JAVA 8 emerged. People can implement a method in an Interface. And as the prevalence of JAVA 8 in more and more organizations. I’d like to highlight how to use Interface static method and default method in Automation Test Design.

Everytime when we design an Automation Test Framework, we need to design various utility classes which provide repetitive functions. And normaly we define static method(s) in a utility class. Take following as example. To use the function, we just simplely write JsonUtil.getJsonFile(“className”).

public class JsonUtil {
    private JsonUtil(){}

    public static String getJsonFile(Class className) throws IOException {

        return new String(Files.readAllBytes(Paths.get(
                new StringBuffer("src/test/resources/testdata/")
                        .append(className.getSimpleName())
                        .append(".json")
                        .toString())), "UTF-8");
    }

    public static String getJsonFile(String fileName) throws IOException {

        return new String(Files.readAllBytes(Paths.get(
                new StringBuffer("src/test/resources/testdata/")
                        .append(fileName)
                        .append(".json")
                        .toString())), "UTF-8");
    }

Actually to prevent users to new an instance of this utility class, we have to define an empty private constructor. If you are using JAVA 8, things are easier like following. And you can call it the same as before JsonUtil.getJsonFile(“className”).

public interface JsonUtil {   

    static String getJsonFile(Class className) throws IOException {

        return new String(Files.readAllBytes(Paths.get(
                new StringBuffer("src/test/resources/testdata/")
                        .append(className.getSimpleName())
                        .append(".json")
                        .toString())), "UTF-8");
    }

    static String getJsonFile(String fileName) throws IOException {

        return new String(Files.readAllBytes(Paths.get(
                new StringBuffer("src/test/resources/testdata/")
                        .append(fileName)
                        .append(".json")
                        .toString())), "UTF-8");
    }

Next let’s discuss default method. Why it is valuable? Trust many JAVA engineers have such experince. When we implement an interface which has lots of defined methods, we only need to implement one or few of them in a specific scenario, but we still have to write an empty implementation for all other methods in that interface. Now if a “default” keyword is added in front of the methods in an interface, the programers who implemt this interface will only need to override the method they really care.

Another benefit interface default method can bring is on refactoring. Previously if we add new methods in an interface, all classes which implement that interface will fail unless they add that new method implementation. That makes it nearly impossible to refactor an interface. Now if you want to add a new method into an interface, JUST DO IT only if you add “default” keyword.

There is only one thing never change in IT world — that is CHANGE. Keep learning and happy learning~~~

Back To Homepage